package com.nhjf.dao.teachers;

import com.nhjf.dao.BaseDao;
import com.nhjf.model.teachers.Teacher;
import com.nhjf.pojo.qo.teachers.TeachersQo;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;



@Repository
public class TeachersDao extends BaseDao<Teacher, TeachersQo> {

	/**
	 * 老师查询
	 */

	@Override
	protected Criteria buildCriteria(Criteria criteria, TeachersQo qo) {
		if(qo!=null){
			criteria.setFetchMode("traininginstitution", qo.isSeachOrganization()?FetchMode.JOIN:FetchMode.SELECT);	
			criteria.setFetchMode("user", qo.isSeachUser()?FetchMode.JOIN:FetchMode.SELECT);	
			if(qo.getId()!=null){
				criteria.setFetchMode("resourceList", FetchMode.SELECT);
			}
			if(StringUtils.isNotBlank(qo.getName())){
				criteria.add(Restrictions.like("name", qo.getName(),MatchMode.ANYWHERE));
			}
			if (StringUtils.isNotBlank(qo.getPhoneNumber())){
				criteria.add(Restrictions.like("phoneNumber",qo.getPhoneNumber(),MatchMode.ANYWHERE));
			}
			
			if(qo.getUserId()!=null) {
				criteria.add(Restrictions.eq("user.id",qo.getUserId()));
			}
			if(qo.isFlas()) {
				criteria.createAlias("user", "user");
				criteria.add(Restrictions.eq("user.itsusableornot",1));
			}
			if(qo.getOrganizationId()!=null) {
				criteria.add(Restrictions.eq("traininginstitution.id",qo.getOrganizationId()));
			}
			if(qo.getIsrest()!=null) {
				criteria.add(Restrictions.eq("isrest",qo.getIsrest()));
			}
			if(qo.getOpenRedraw()!=null) {
				criteria.add(Restrictions.eq("openRedraw",qo.getOpenRedraw()));
			}
			if(qo.getOpenTutor()!=null) {
				criteria.add(Restrictions.eq("openTutor",qo.getOpenTutor()));
			}
			if(qo.getTeacherId()!=null) {
				criteria.add(Restrictions.eq("id",qo.getTeacherId()));
			}
			if(StringUtils.isNotBlank(qo.getNickname())) {
	
				criteria.add(Restrictions.like("nickname", qo.getNickname(),MatchMode.ANYWHERE));
			}
			if(qo.getOrderOpenRedraw()!=null) {
				criteria.addOrder(Order.desc("openRedraw"));
			}
			if(qo.getOrderOpenTutor()!=null) {
				criteria.addOrder(Order.desc("openTutor"));
			}
			if(StringUtils.isNotBlank(qo.getOrderString())) {
				criteria.addOrder(Order.desc(qo.getOrderString()));
			}
			criteria.addOrder(Order.asc("registrationTime"));
		}

		return criteria;
	}

	@Override
	protected Class<Teacher> getEntityClass() {
		return Teacher.class;
	}
}
